/**
 * 
 */
package rmi;

import java.rmi.Naming;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author REMY Timothée and CARTIER Arthur
 */
public class Me extends User {

	/**
	 * @author arthur
	 * 
	 */
	public class connectDisconnectedFriend implements Callable<Boolean> {
		UserBuffer friendToConnect;

		public connectDisconnectedFriend(final UserBuffer friendToConnect) {
			super();
			this.friendToConnect = friendToConnect;
		}

		/**
		 * @see java.util.concurrent.Callable#call()
		 * @return
		 * @throws Exception
		 */
		@Override
		public Boolean call() throws Exception {
			UserInterface distantUser = null;

			if (friendToConnect.getIp() != null
					&& !friendToConnect.getIp().isEmpty()
					&& friendToConnect.getPort() > 0) {
				final ConnectionInterface distantConnection = (ConnectionInterface) Naming
						.lookup("//" + friendToConnect.getIp() + ":"
								+ friendToConnect.getPort() + "/connect");
				if (distantConnection != null) {
					distantUser = distantConnection.connect(getMe(),
							getConnection());
				}
			}

			if (distantUser == null) {
				distantUser = searchFriend(friendToConnect.getPseudo(), 10);
			}

			if (distantUser != null) {
				final ConnectionInterface distantConnection = (ConnectionInterface) Naming
						.lookup("//" + distantUser.getIp() + ":"
								+ distantUser.getPort() + "/connect");
				if (distantConnection != null) {
					friendToConnect._setDistantUser(distantConnection.connect(
							getMe(), getConnection()));
				}
			}

			return (distantUser != null);
		}

	}

	private static final long serialVersionUID = 8097574072030718881L;

	HashMap<String, UserBuffer> friends = new HashMap<String, UserBuffer>();

	private Connection connection;

	private ArrayList<String> blacklist = new ArrayList<String>();

	private ArrayList<Discussion> currentDiscussions = new java.util.ArrayList<Discussion>();

	private String saveMeFile;
	private String saveDiscussionsFolder;

	/**
	 * @throws RemoteException
	 */
	public Me() throws RemoteException {
		setMe(this);
	}

	/**
	 * @param pseudo
	 * @param ip
	 * @param port
	 * @throws RemoteException
	 */
	public Me(final String pseudo, final String ip, final int port)
			throws RemoteException {
		super(pseudo, ip, port, null);
		setMe(this);
	}

	/**
	 * @param connection
	 *            the connection to set
	 */
	public void _setConnection(final Connection connection) {
		this.connection = connection;
	}

	/**
	 * @param saveMeFile
	 *            the saveMeFile to set
	 */
	public void _setSaveMeFile(final String saveMeFile) {
		this.saveMeFile = saveMeFile;
	}

	public void connectAllDisconnectedFriends() {
		final ExecutorService executor = Executors.newCachedThreadPool();
		for (final UserBuffer currentFriend : friends.values()) {
			final connectDisconnectedFriend cdf = new connectDisconnectedFriend(
					currentFriend);
			executor.submit(cdf);
		}
	}

	/**
	 * @return the blacklist
	 */
	public ArrayList<String> getBlacklist() {
		return blacklist;
	}

	/**
	 * @return the connection
	 */
	public Connection getConnection() {
		return connection;
	}

	/**
	 * @return the currentDiscussions
	 */
	public ArrayList<Discussion> getCurrentDiscussions() {
		return currentDiscussions;
	}

	/**
	 * @return the friends
	 */
	public HashMap<String, UserBuffer> getFriends() {
		return friends;
	}

	/**
	 * @return the saveDiscissionsFolder
	 */
	public String getSaveDiscussionsFolder() {
		return saveDiscussionsFolder;
	}

	/**
	 * @return the saveMeFile
	 */
	public String getSaveMeFile() {
		return saveMeFile;
	}

	/**
	 * @param blacklist
	 *            the blacklist to set
	 */
	public void setBlacklist(final ArrayList<String> blacklist) {
		this.blacklist = blacklist;
	}

	/**
	 * @param currentDiscussions
	 *            the currentDiscussions to set
	 */
	@SuppressWarnings("unused")
	private void setCurrentDiscussions(
			final ArrayList<Discussion> currentDiscussions) {
		this.currentDiscussions = currentDiscussions;
	}

	/**
	 * @param friends
	 *            the friends to set
	 */
	public void setFriends(final HashMap<String, UserBuffer> friends) {
		this.friends = friends;
	}

	/**
	 * @param saveDiscissionsFolder
	 *            the saveDiscissionsFolder to set
	 */
	public void setSaveDiscussionsFolder(final String saveDiscissionsFolder) {
		saveDiscussionsFolder = saveDiscissionsFolder;
	}

}
